package com.lfy.fyadmin.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Sa-Token配置类
 */
@Configuration
public class SaTokenConfig implements WebMvcConfigurer {
    
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册Sa-Token拦截器
        registry.addInterceptor(new SaInterceptor(handler -> {
            // 登录校验 -- 拦截所有路由，并排除登录相关的路由
            SaRouter.match("/**")
                   .notMatch("/api/auth/login", "/api/auth/captcha", "/api/auth/logout")
                   .notMatch("/doc.html", "/doc.html/**")
                   .notMatch("/swagger-ui.html","/swagger-ui/**", "/v3/api-docs/**")
                   .notMatch("/webjars/**", "/favicon.ico")
                   .notMatch("/error")
                   .check(r -> StpUtil.checkLogin());
            
        })).addPathPatterns("/**");
    }
}